package com.wzq.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Result;
import com.wzq.dao.pojo.Sysnumber;
import com.wzq.dao.pojo.Wechat_user;
import com.wzq.service.ISysnumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/PC/sys")
public class PCSysnumberController extends GetSome {

    @Autowired
    private ISysnumberService iSys_numberService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Sysnumber sys_number) {
        boolean flag = iSys_numberService.save(sys_number);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Sysnumber sys_number) {
        boolean flag = iSys_numberService.updateById(sys_number);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iSys_numberService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{number}/yz/{password}")
    public Result getByOneTitle(@PathVariable String number ,
                                @PathVariable String password
    ) {
        LambdaQueryWrapper<Sysnumber> s_lqw = new LambdaQueryWrapper<Sysnumber>().eq(Sysnumber::getNumber, number).eq(Sysnumber::getPassword, password);
        Sysnumber sysnumber = iSys_numberService.getOne(s_lqw);
        if(sysnumber!=null){
            LambdaQueryWrapper<Wechat_user> w_lqw = new LambdaQueryWrapper<>();
            w_lqw.eq(Wechat_user::getSys_number , sysnumber.getId());
            Wechat_user one = iWechat_userService.getOne(w_lqw);
            if(one!=null)
            return new Result(Code.GET_OK,one,"登陆成功");
        }
        return new Result(Code.GET_ERR,  number ,"登陆失败");
    }
    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Sysnumber> list = iSys_numberService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

}
